package com.huatai.vba.execute.action;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huatai.vba.common.utils.*;
import com.huatai.vba.dal.jdbc.DBUtils;
import com.huatai.vba.dal.model.VbaFileAction;
import com.huatai.vba.properties.Dbconfig;
import com.huatai.vba.properties.VbaEnv;
import com.huatai.vba.service.VbaExtendsService;

@Service("initFileCopyActionExecutor")
public class InitFileCopyActionExecutor extends AbstractExcutor implements ActionExecutor {

	private static final Logger logger = LoggerFactory.getLogger(InitFileCopyActionExecutor.class);

	@Autowired
	protected VbaExtendsService vbaExtendsService;

	@Override
	public void execute(VbaFileAction action) {
		String disc = VbaEnv.isTest ? "D" : "H";
		Date d = new Date();
		String yyyyMMdd = new SimpleDateFormat("yyyyMMdd").format(d);
		String yymm = new SimpleDateFormat("yyMM").format(d);
		String seatmpFilePath = disc + ":/pro_data/prg/seatmap";
		createSeatmap(seatmpFilePath);

		// cope 文件
		String year = yyyyMMdd.substring(0, 4);
		String fileDir = yyyyMMdd.substring(2, 8);
		File dst = new File(disc + ":/remote/dbf/" + year + "/" + yymm + "/" + fileDir + "/");
		File dst1 = new File(disc + ":/szhq/" + year + "/" + yymm + "/" + fileDir + "/");

		File dst2 = new File(disc + ":/vsat3/dbf/" + year + "/" + yymm + "/" + fileDir + "/");
		// 20161011 深交所新接口行情文件,复制到深圳行情文件夹里
		File dst3 = new File(disc + ":/szhq/" + year + "/" + yymm + "/" + yyyyMMdd.substring(2, 8) + "/");
		copyShow(dst);
		copyDbfFile(dst);
		copyMsgFile(dst);
		copyGzlx(dst, d);
		copyDstStr1(dst1);
		copyDstStr2(dst2);
		copyDst3(dst3);
	}

	private void createSeatmap(String seatmpFilePath) {
		try {
			File seatmpFile = new File(seatmpFilePath);
			if (seatmpFile.exists()) {
				seatmpFile.delete();
			}
			String sql = "select b.vc_fund_code, b.vc_fund_name, a.vc_seat_id "
					+ " from ttradeseat a, tfundinfo b where a.l_fund_id = b.l_fund_id"
					+ "  and a.c_market_no = '1' and a.c_trade = '1' order by b.vc_fund_code asc";
			List<Map<String, Object>> list = DBUtils.executeQuery(Dbconfig.TRADEDB, "trade_zc",
					Dbconfig.getPasswordByUserId("trade_zc"), sql);
			List<String> contents = new ArrayList<String>();
			if (ListUtils.isNotEmpty(list)) {
				for (Map<String, Object> m : list) {
					StringBuffer content = new StringBuffer();
					content.append(m.get("vc_fund_code".toUpperCase()));
					content.append(",");
					content.append(m.get("vc_fund_name".toUpperCase()));
					content.append(",");
					content.append(m.get("vc_seat_id".toUpperCase()));
					contents.add(content.toString());
					contents.add("");
				}
				FileUtils.writeLines(seatmpFile, contents);
			}
		} catch (Exception ex) {
			logger.error("系统异常", ex);
		}

	}

	private void copyDst3(File dst) {
		try {
			String yyyyMMdd = new SimpleDateFormat("yyyyMMdd").format(new Date());
			String strPathSource = "H:/szhq/szhqnew/";
			File[] files3 = FileUtils.findFiles(new File(strPathSource), "*" + yyyyMMdd + "*");
			FileUtils.copy(files3, dst);
			System.out.println("=====复制Dst3文件成功====，文件个数为：" + files3.length);
		} catch (Exception ex) {
			logger.error("系统异常", ex);
		}
	}

	private void copyDstStr2(File dst) {
		try {
			FileUtils.copy(new File("h:/vsat3/LOFJS.dbf"), dst);
			FileUtils.copy(new File("h:/vsat3/SJSDZ.dbf"), dst);
			FileUtils.copy(new File("h:/vsat3/SJSGF.dbf"), dst);
			FileUtils.copy(new File("h:/vsat3/SJSHB.dbf"), dst);
			FileUtils.copy(new File("h:/vsat3/SJSQS.dbf"), dst);
			FileUtils.copy(new File("h:/vsat3/SJSTJ.dbf"), dst);
			FileUtils.copy(new File("h:/vsat3/SJSWT.dbf"), dst);
			FileUtils.copy(new File("h:/vsat3/SJSZJ.dbf"), dst);
			FileUtils.copy(new File("h:/vsat3/SJSGB.dbf"), dst);
			FileUtils.copy(new File("h:/vsat3/SJSJG.dbf"), dst);
			FileUtils.copy(new File("h:/vsat3/SJSJX.dbf"), dst);
			FileUtils.copy(new File("h:/vsat3/SJSKS.dbf"), dst);
			FileUtils.copy(new File("h:/vsat3/SJSQS0.dbf"), dst);
			FileUtils.copy(new File("h:/vsat3/SJSTP.dbf"), dst);
			FileUtils.copy(new File("h:/vsat3/SJSYE.dbf"), dst);

			// 从dcom复制过来
			String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
			String strPathSource = "h:/dcom/" + date + "/";
			String mmdd = new SimpleDateFormat("MMdd").format(new Date());

			FileUtils.copy(new File(strPathSource + "SJSMX0" + mmdd + ".dbf"), dst);
			FileUtils.copy(new File(strPathSource + "SJSMX1" + mmdd + ".dbf"), dst);
			FileUtils.copy(new File(strPathSource + "SJSMX2" + mmdd + ".dbf"), dst);
			System.out.println("=====复制DstStr2文件成功====");
		} catch (Exception ex) {
			logger.error("系统异常", ex);
		}
	}

	private void copyDstStr1(File dst) {
		try {
			FileUtils.copy(new File("h:/szhq/sjshq.dbf"), dst);
			System.out.println("=====复制sjshq.dbf文件成功====");
			FileUtils.copy(new File("h:/szhq/sjsxx.dbf"), dst);
			System.out.println("=====复制sjsxx.dbf文件成功====");
			FileUtils.copy(new File("h:/szhq/sjsxxn.dbf"), dst);
			System.out.println("=====复制sjsxxn.dbf文件成功====");
			FileUtils.copy(new File("h:/szhq/sjszs.dbf"), dst);
			System.out.println("=====复制sjszs.dbf文件成功====");
		} catch (Exception ex) {
			logger.error("系统异常", ex);
		}
	}

	private void copyShow(File dst) {
		try {
			FileUtils.copy(new File("h:/remote/dbf/show2003.dbf"), dst);
			System.out.println("=====复制show2003.dbf文件成功====");
		} catch (Exception ex) {
			logger.error("系统异常", ex);
		}

	}

	private void copyDbfFile(File dst) {
		String date = new SimpleDateFormat("yyyyMMdd").format(new Date());
		File fld = new File("h:/remote/dbf/");

		File[] files1 = FileUtils.findFiles(fld, "gh*.dbf");
		File[] files2 = FileUtils.findFiles(fld, "bgh*.dbf");
		File[] files3 = FileUtils.findFiles(fld, "ipogh*.txt");
		// 上交所fast行情
		// **************************************************************************************
		File[] files4 = FileUtils.findFiles(fld, "mktdt*.txt");
		File[] files5 = FileUtils.findFiles(fld, "fjy*" + date + "*.txt");
		File[] files6 = FileUtils.findFiles(fld, "cpxx*.txt");
		// 固定收益平台
		File[] files7 = FileUtils.findFiles(fld, "zgh040*.dbf");
		try {
			FileUtils.copy(files1, dst);
			FileUtils.copy(files2, dst);
			FileUtils.copy(files3, dst);
			FileUtils.copy(files4, dst);
			FileUtils.copy(files5, dst);
			FileUtils.copy(files6, dst);
			FileUtils.copy(files7, dst);
			int num = files1.length + files2.length + files3.length + files4.length + files5.length + files6.length
					+ files7.length;
			System.out.println("复制文件成功，总共复制了文件个数为" + num);
		} catch (Exception ex) {
			logger.error("系统异常", ex);
		}
	}

	private void copyMsgFile(File dst) {
		String date = new SimpleDateFormat("MMdd").format(new Date());
		File fld = new File("h:/remote/msg/");
		File[] files1 = FileUtils.findFiles(fld, "trdses*.txt");
		File[] files2 = FileUtils.findFiles(fld, "exra" + date + "*.txt");
		File[] files3 = FileUtils.findFiles(fld, "reff" + date + "*.txt");
		File[] files4 = FileUtils.findFiles(fld, "zxjc" + date + "*.txt");
		try {
			FileUtils.copy(files1, dst);
			FileUtils.copy(files2, dst);
			FileUtils.copy(files3, dst);
			FileUtils.copy(files4, dst);
			System.out.println("复制文件成功，总共复制了文件个数为" + (files1.length + files2.length + files3.length + files4.length));
		} catch (Exception ex) {
			logger.error("系统异常", ex);
		}

	}

	private void copyGzlx(File dst, Date copyDate) {
		try {
			String date = new SimpleDateFormat("MMdd").format(copyDate);
			String strMonth = DateUtils.convertExaMonth(date.substring(0, 2));
			String strDay = date.substring(2, 4);
			String fileName = "GZLX." + strMonth + strDay;
			FileUtils.copy(new File("h:/remote/msg/" + fileName), dst);
		} catch (Exception ex) {
			logger.error("系统异常", ex);
		}
	}

}
